<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        const vehicleList = [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8,
            9,
            10,
            11,
            12,
            13,
            14,
            15,
            16
        ];
        const newList = [
            {
                "id": 1,
                "type": "sys",
                "name": "vCloud",
                "children": [
                    {
                        "id": 1,
                        "type": "mod",
                        "name": "事件采集",
                        "children": [
                            {
                                "id": 2,
                                "type": "spe",
                                "name": "创建",
                                "children": null
                            },
                            {
                                "id": 3,
                                "type": "spe",
                                "name": "复制",
                                "children": null
                            },
                            {
                                "id": 4,
                                "type": "spe",
                                "name": "修改",
                                "children": null
                            },
                            {
                                "id": 5,
                                "type": "spe",
                                "name": "下载",
                                "children": null
                            },
                            {
                                "id": 6,
                                "type": "spe",
                                "name": "重试",
                                "children": null
                            },
                            {
                                "id": 7,
                                "type": "spe",
                                "name": "批量重试",
                                "children": null
                            },
                            {
                                "id": 8,
                                "type": "spe",
                                "name": "全部重试",
                                "children": null
                            },
                            {
                                "id": 9,
                                "type": "spe",
                                "name": "停止",
                                "children": null
                            },
                            {
                                "id": 10,
                                "type": "spe",
                                "name": "批量停止",
                                "children": null
                            },
                            {
                                "id": 11,
                                "type": "spe",
                                "name": "全部停止",
                                "children": null
                            }
                        ]
                    },
                    {
                        "id": 14,
                        "type": "mod",
                        "name": "诊断指令下发",
                        "children": [
                            {
                                "id": 15,
                                "type": "spe",
                                "name": "部署/保存",
                                "children": null
                            },
                            {
                                "id": 16,
                                "type": "spe",
                                "name": "停止",
                                "children": null
                            }
                        ]
                    },
                    {
                        "id": 12,
                        "type": "mod",
                        "name": "用户管理",
                        "children": null
                    },
                    {
                        "id": 13,
                        "type": "mod",
                        "name": "角色管理",
                        "children": null
                    }
                ]
            }
        ]

        function findMatchingIds(item, vehicleList) {
            let matchedIds = [];

            // 检查当前节点的 id
            if (vehicleList.includes(item.id)) {
                matchedIds.push(item.id);
            }

            // 如果有子节点并且子节点不是 null，递归检查子节点
            if (item.children && item.children.length > 0) {
                item.children.forEach(child => {
                    const childIds = findMatchingIds(child, vehicleList);
                    matchedIds = matchedIds.concat(childIds);
                });
            }

            return matchedIds;
        }

        const allMatchedIds = newList.reduce((acc, item) => {
            const itemIds = findMatchingIds(item, vehicleList);
            return acc.concat(itemIds);
        }, []);

        console.log(allMatchedIds); // 输出匹配的 id 数组
    </script>
</body>

</html>